<?php defined('SYSPATH') OR die('No direct access allowed.');
 /**
 * MY_Controller. Base Controller.
 * 
 */
class Controller extends Controller_Core
{
	protected $session; // Session
	protected $view; // View 
	protected $user_model; // User Model
 
    public function __construct()
    {
        parent::__construct(); // This must be included
 
        $this->session = Session::instance(); // instance Session
		$this->user_model = new User_Model;
    }	
	
	/**
	 * Otentikasi 
	 * Mengecek apakah user sudah login atau belum (dicek pada session)
	 * Jika session dengan key username sudah diset -> user sudah login
	 */
	protected function _authenticate()
	{
		if (!$this->session->get('username')) 
		{			
			$this->view = new View('error');
			$this->view->errors = array(0=>'Maaf, Anda harus login terlebih dahulu');
			$this->view->render(TRUE);
			return false;
		}
		return true;
	}
	
	/**
	 * Parameter title string 
	 * Title merupakan judul halaman
	 * Parameter main string 
	 * main merupakan View pada halaman 
	 * View dengan nama main harus ada pada folder application/views
	 * Parameter error string atau array of string
	 * error merupakan pesan kesalahan
	 *
	 * Membuat view default untuk tampilan aplikasi
	 */
	protected function _loadContent($title = NULL, $content = NULL)
	{
		$namasitus = "HanSIP"; // Nama situs
		$this->view = new View('template'); 
		
		// Title/judul
		$this->view->title = $title;

        // Halaman main
		$this->view->content = ($content != NULL) ? new View($content) : NULL;

        // Sidebar
        $this->view->sidebar = new View('sidebar');
        if($this->session->get('id',false)) {
            $user = $this->user_model->get_user($this->session->get('id'));
            if($user->current()->ID_role==1) {  //ID = Administrator
                $this->view->sidebar->links = array(
                        "Dashboard"     => "dashboard",
                        "Menu"          => "menu",
                        "Lokasi"        => "lokasi",
                        "Stok"          => "stok",
                        "User"          => "user",
                        "Produksi"		=> "produksi",
                        "Inventarisasi" => "inventarisasi",
                        "Pembukuan"     => "pembukuan"
                    );
            } else if($user->current()->ID_role==2) {   //ID = owner
                $this->view->sidebar->links = array(
                        "Dashboard"     => "dashboard",
                        "Menu"          => "menu",
                        "Lokasi"        => "lokasi",
                        "Stok"          => "stok",
                        "Produksi"		=> "produksi",
                        "Inventarisasi" => "inventarisasi",
                        "Pembukuan"     => "pembukuan"
                    );
            } else if($user->current()->ID_role==3) {   //ID = manajer keuangan
                $this->view->sidebar->links = array(
                        "Dashboard"     => "dashboard",
                        "Inventarisasi" => "inventarisasi",
                        "Pembukuan"     => "pembukuan"
                    );
            } else if($user->current()->ID_role==4) {   //ID = manajer produksi
                $this->view->sidebar->links = array(
                        "Dashboard"     => "dashboard",
                        "Menu"          => "menu",
                        "Lokasi"        => "lokasi",
                        "Stok"          => "stok",
                        "User"          => "user",
                        "Produksi"		=> "produksi"
                    );
            }
        }
	}
}
